package com.qianfeng.smartdevice.controller;

import com.github.pagehelper.PageInfo;
import com.qianfeng.smartdevice.dto.R;
import com.qianfeng.smartdevice.pojo.SysCategory;
import com.qianfeng.smartdevice.service.SysCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by wenka on 2022/3/25 15:27
 *
 * @author Jackiechan
 * @version 1.0
 * @since 1.0
 */
@RestController
@RequestMapping("/categories")
public class SysCategoryController {
    private SysCategoryService sysCategoryService;

    @Autowired
    public void setSysCategoryService(SysCategoryService sysCategoryService) {
        this.sysCategoryService = sysCategoryService;
    }
    @GetMapping("/category/{id}")
    public R findById(@PathVariable Long id){
        SysCategory byCid = sysCategoryService.findByCid(id);
        return R.setOK(byCid);
    }
    @PostMapping("/category")
    public R addCategory(@RequestBody SysCategory sysCategory){
        sysCategoryService.addCategory(sysCategory);
        return R.steOK();
    }
    @GetMapping("/categories")
    public R findAllByPage(@RequestParam(defaultValue = "1",name = "page") int pageNum,@RequestParam(defaultValue = "1",name = "limit")  int pageSize, String categoryName) {
        PageInfo<SysCategory> pageInfo = sysCategoryService.findAll(pageNum, pageSize, categoryName);
        return R.setOK(pageInfo);
    }
    @PostMapping("/category/delete")
    public R deleteById(@RequestBody List<Long> ids){
        int count = sysCategoryService.deleteByIds(ids);
        return R.setOK(count);
    }
    @PostMapping("/category/edit")
    public R updateCategory(@RequestBody SysCategory sysCategory){
        int count = sysCategoryService.updateCategory(sysCategory);
        return R.setOK(count);
    }

}
